package com.softstao.guoyu.ui.activity.me;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.APIInterface;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.Message;
import com.softstao.guoyu.ui.activity.MainActivity;
import com.softstao.guoyu.ui.activity.agent.AuditListActivity;
import com.softstao.guoyu.ui.activity.sale.OrderDetailActivity;
import com.softstao.softstaolibrary.library.widget.ProgressWebView;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;

/**
 * Created by vaikus on 2017/7/27.
 */

public class MessageActivity extends BaseActivity {

    @BindView(R.id.web_view)
    ProgressWebView webView;
    private String url;

    @Override
    public int _ContentView() {
        return R.layout.activity_web;
    }

    @SuppressLint("JavascriptInterface")
    @Override
    public void initView() {
        initTitle("消息");
        url = APIInterface.SERVER_WAP + "msgConter.html?agentId=" + SharePreferenceManager.getInstance().getAgentId();
        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.addJavascriptInterface(new Object(){
            @JavascriptInterface           // For API 17+
            public void performClick(String strl) {
            }
        },"ok");
        this.webView.getSettings().setDefaultTextEncodingName("UTF-8");
        this.webView.getSettings().setAllowFileAccess(true);
        this.webView.getSettings().setLoadWithOverviewMode(true);
        this.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        this.webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("guoyu://")) {
                    return analysisUrl(url);
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
    }

    private boolean analysisUrl(String url) {
        Pattern p = Pattern.compile("guoyu://(\\w+)\\?(.+)");
        Matcher m = p.matcher(url);
        if (m.find()) {
            String content = m.group();
            Map<String, String> map = new HashMap<>();
            String[] strings1 = content.split("//");
            String[] app = (strings1[1]).split("\\?");
            map.put("app", app[0]);
            String[] strings2 = (app[1]).split("&");
            for (int i = 0; i < strings2.length; i++) {
                String[] strings3 = strings2[i].split("=");
                map.put(strings3[0], strings3[1]);
            }
            dealLink(map);
            return true;
        }
        return false;
    }

    private void dealLink(Map<String, String> map) {
        String app = map.get("app");
        String content = map.get("id");
        Message.Type type = Message.Type.valueOf(app.toUpperCase());
        switch (type) {
            case ORDERFROM:
                Intent fromIntent = new Intent(context, OrderDetailActivity.class);
                fromIntent.putExtra("order_title","to_order");
                fromIntent.putExtra("order_code",content);
                startActivity(fromIntent);
                break;
            case ORDERTRANSFER:
                Intent transIntent = new Intent(context, OrderDetailActivity.class);
                transIntent.putExtra("order_title","transfer");
                transIntent.putExtra("order_code",content);
                startActivity(transIntent);
                break;
            case ORDERTO:
                Intent toIntent = new Intent(context, OrderDetailActivity.class);
                toIntent.putExtra("order_title","from_order");
                toIntent.putExtra("order_code",content);
                startActivity(toIntent);
                break;
            case AGENT:
                startActivity(new Intent(context, MainActivity.class).putExtra("index","1"));
                break;
            case AUDIT:
                startActivity(new Intent(context, AuditListActivity.class));
                break;
            case RECHARGE:
                startActivity(new Intent(context, BalanceActivity.class));
                break;
            case NOTICE:
                break;
        }
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {//监听返回键，如果可以后退就后退
            if (webView.canGoBack()) {
                webView.goBack();
                return true;

            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.removeAllViews();
        webView.destroy();
    }
}
